package com.aierkeji.call.b2c.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.aierkeji.call.b2c.entity.AkjCallApprovalHistroVo;
import com.aierkeji.call.b2c.gson.DateTypeAdapter;
import com.aierkeji.call.b2c.gson.TimestampTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class GsonUtils {

public static Gson gson = new GsonBuilder()
.registerTypeAdapter(Timestamp.class,new TimestampTypeAdapter())
.registerTypeAdapter(Date.class, new DateTypeAdapter())
		.addSerializationExclusionStrategy(new SpringPageExclusion()).create();

	public static <T> T fromJson(JsonObject json, Class<T> classOfT){
		return gson.fromJson(json, classOfT);
	}

	public static <T> T fromJson(String json, Class<T> classOfT){
		return gson.fromJson(json, classOfT);
	}

	public static <T> T fromJson(String json,Type typeOfT){
		return gson.fromJson(json,typeOfT);
	}

	public static JsonObject parseToJsonObj(String jsonstr){
		return new JsonParser().parse(jsonstr).getAsJsonObject();
	}

	public static String getString(JsonObject jsonObj,String key){
		if(jsonObj.has(key)){
			return jsonObj.get(key).getAsString();
		}
		return null;
	}
	
	public static <T> T transMapToObj(Map map, Class<T> classOfT) {
	    String paramJsonString = gson.toJson(map);
	    return gson.fromJson(paramJsonString, classOfT);

	}

	public static String parseToJson(Object obj){
		return gson.toJson(obj);
	}

	public static JsonArray getArray(JsonObject jsonObj,String key){
		if(jsonObj.has(key) && jsonObj.get(key).isJsonArray()){
			return jsonObj.get(key).getAsJsonArray();
		}
		return null;
	}

	public static JsonObject getObj(JsonObject jsonObj,String key){
		if(jsonObj.has(key) && jsonObj.get(key).isJsonObject()){
			return jsonObj.get(key).getAsJsonObject();
		}
		return null;
	}

}

